package com.ddzh.web;

import com.ddzh.entity.Result;
import com.ddzh.service.OrderSettingService;
import com.ddzh.utils.POIUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/orderSetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    /**
     * 上传文件，-》解析Excel表格，存入数据库
     * @param file
     * @return 返回表格数据的list集合
     */
    @PostMapping("/import")
    public Result importData(@RequestParam("file") MultipartFile file){
        List<String[]> data =null;
        try {
            data = POIUtils.readExcel(file.getInputStream(), file.getOriginalFilename());
        } catch (IOException e) {
            e.printStackTrace();
        }
        //将表格数据存入数据库
        orderSettingService.importData(data);
        return new Result(data);
    }

    /**
     * 每次查询一个月的记录：需要参数年月
     * @return
     */
    @GetMapping("/selectDataByMonth/{year}/{month}")
    public Result selectDataByMonth(@PathVariable("year") int year,@PathVariable("month") int month){
        return new Result(orderSettingService.selectDataByMonth(year,month));
    }

    @GetMapping("/daySetting/{day}/{num}")
    public Result daySetting(@PathVariable("day") String day,@PathVariable("num") int num){
        return new Result(orderSettingService.daySetting(day,num));
    }
//
//    @PostMapping("/import")
//    public Result importData(@RequestParam("file") MultipartFile file){
//        List<String[]> data = null;
//        try {
//            data = POIUtils.readExcel(file.getInputStream(),file.getOriginalFilename());
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return new Result(orderSettingService.importData(data));
//    }
//
//    @GetMapping("/selectDataByMonth/{year}/{month}")
//    public Result selectDataByMonth(@PathVariable("year") int year, @PathVariable("month") int month){
//        return new Result(orderSettingService.selectDataByMonth(year,month));
//    }
//
//    @GetMapping("/daySetting/{day}/{num}")
//    public Result daySetting(@PathVariable("day") String day, @PathVariable("num") Integer num){
//        return new Result(orderSettingService.daySetting(day,num));
//    }

}
